package com.umeng.analytics.pro;

import android.content.Context;
import android.content.SharedPreferences;

import com.umeng.analytics.AnalyticsConfig;

import java.io.File;

import org.json.JSONObject;

public class aa {
    private final byte[] a;
    private final int b;
    private final int c;
    private String d;
    private String e;
    private byte[] f;
    private byte[] g;
    private byte[] h;
    private int i;
    private int j;
    private int k;
    private byte[] l;
    private byte[] m;
    private boolean n;

    private aa(byte[] arg5, String arg6, byte[] arg7) throws Exception {

        this.a = new byte[]{0, 0, 0, 0, 0, 0, 0, 0};
        this.b = 1;
        this.c = 0;
        this.d = "1.0";
        this.e = null;
        this.f = null;
        this.g = null;
        this.h = null;
        this.i = 0;
        this.j = 0;
        this.k = 0;
        this.l = null;
        this.m = null;
        this.n = false;
        if (arg5 != null && arg5.length != 0) {
            this.e = arg6;
            this.k = arg5.length;
            this.l = bu.a(arg5);
            this.j = ((int) (System.currentTimeMillis() / 1000));
            this.m = arg7;
            return;
        }

        throw new Exception("entity is null or empty");
    }

    public static aa a(Context arg8, String arg9, byte[] arg10) {
        aa v0_1;
        aa v1 = null;
        try {
            String v2 = bv.q(arg8);
            String v3 = bv.c(arg8);
            SharedPreferences v4 = ba.a(arg8);
            String v5 = v4.getString("signature", null);
            int v6 = v4.getInt("serial", 1);
            v0_1 = new aa(arg10, arg9, (v3 + v2).getBytes());
            v0_1.a(v5);
            v0_1.a(v6);
            v0_1.b();
            v4.edit().putInt("serial", v6 + 1).putString("signature", v0_1.a()).commit();
            v0_1.b(arg8);
        } catch (Exception v0) {
            v0.printStackTrace();
            v0_1 = v1;
        }

        return v0_1;
    }

    public void a(String arg2) {
        this.f = bt.a(arg2);
    }

    public void a(int arg1) {
        this.i = arg1;
    }

    public String a() {
        return bt.a(this.f);
    }

    public static String a(Context arg3) {
        String v0 = null;
        SharedPreferences v1 = ba.a(arg3);
        if (v1 != null) {
            v0 = v1.getString("signature", v0);
        }

        return v0;
    }

    private byte[] a(byte[] arg10, int arg11) {
        int v8 = 2;
        int v0 = 0;
        byte[] v2 = bt.b(this.m);
        byte[] v3 = bt.b(this.l);
        int v4 = v2.length;
        byte[] v5 = new byte[v4 << 1];
        int v1;
        for (v1 = 0; v1 < v4; ++v1) {
            v5[v1 << 1] = v3[v1];
            v5[(v1 << 1) + 1] = v2[v1];
        }

        for (v1 = 0; v1 < v8; ++v1) {
            v5[v1] = arg10[v1];
            v5[v5.length - v1 - 1] = arg10[arg10.length - v1 - 1];
        }

        byte[] v1_1 = new byte[4];
        v1_1[0] = ((byte) arg11);
        v1_1[1] = ((byte) (arg11 >> 8));
        v1_1[v8] = ((byte) (arg11 >> 16));
        v1_1[3] = (byte) (arg11 >> 24);
        while (v0 < v5.length) {
            v5[v0] = ((byte) (v5[v0] ^ v1_1[v0 % 4]));
            ++v0;
        }

        return v5;
    }

    public void a(boolean arg1) {
        this.n = arg1;
    }

    public void b() {
        int v1 = 16;
        if (this.f == null) {
            this.f = this.d();
        }

        if (this.n) {
            byte[] v0 = new byte[v1];
            try {
                System.arraycopy(this.f, 1, v0, 0, 16);
                this.l = bt.a(this.l, v0);
            } catch (Exception v0_1) {
            }
        }

        this.g = this.a(this.f, this.j);
        this.h = this.e();
    }

    public void b(Context arg9) {
        String v1 = this.e;
        String v2 = af.a(arg9).b().g(null);
        String v0 = bt.a(this.f);
        byte[] v3 = new byte[16];
        System.arraycopy(this.f, 2, v3, 0, 16);
        String v3_1 = bt.a(bt.b(v3));
        try {
            JSONObject v4 = new JSONObject();
            v4.put("appkey", v1);
            if (v2 != null) {
                v4.put("umid", v2);
            }

            v4.put("signature", v0);
            v4.put("checksum", v3_1);
            File v0_2 = new File(arg9.getFilesDir(), ".umeng");
            if (!v0_2.exists()) {
                v0_2.mkdir();
            }

            bw.a(new File(v0_2, "exchangeIdentity.json"), v4.toString());
        } catch (Throwable v0_1) {
            v0_1.printStackTrace();
        }

        try {
            JSONObject v0_3 = new JSONObject();
            v0_3.put("appkey", v1);
            v0_3.put("channel", AnalyticsConfig.getChannel(arg9));
            if (v2 != null) {
                v0_3.put("umid", bw.b(v2));
            }

            bw.a(new File(arg9.getFilesDir(), "exid.dat"), v0_3.toString());
        } catch (Throwable v0_1) {
            v0_1.printStackTrace();
        }
    }

    public static aa b(Context arg8, String arg9, byte[] arg10) {
        aa v0_1;
        aa v1 = null;
        try {
            String v2 = bv.q(arg8);
            String v3 = bv.c(arg8);
            SharedPreferences v4 = ba.a(arg8);
            String v5 = v4.getString("signature", null);
            int v6 = v4.getInt("serial", 1);
            v0_1 = new aa(arg10, arg9, (v3 + v2).getBytes());
            v0_1.a(true);
            v0_1.a(v5);
            v0_1.a(v6);
            v0_1.b();
            v4.edit().putInt("serial", v6 + 1).putString("signature", v0_1.a()).commit();
            v0_1.b(arg8);
        } catch (Exception v0) {
            v0.printStackTrace();
            v0_1 = v1;
        }

        return v0_1;
    }

    public byte[] c() {
        byte[] v0_2;
        bq v1 = new bq();
        v1.a(this.d);
        v1.b(this.e);
        v1.c(bt.a(this.f));
        v1.a(this.i);
        v1.c(this.j);
        v1.d(this.k);
        v1.a(this.l);
        int v0 = this.n ? 1 : 0;
        v1.e(v0);
        v1.d(bt.a(this.g));
        v1.e(bt.a(this.h));
        try {
            v0_2 = new cp().a(((cg) v1));
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
            v0_2 = null;
        }

        return v0_2;
    }

    private byte[] d() {
        return this.a(this.a, ((int) (System.currentTimeMillis() / 1000)));
    }

    private byte[] e() {
        StringBuilder v0 = new StringBuilder();
        v0.append(bt.a(this.f));
        v0.append(this.i);
        v0.append(this.j);
        v0.append(this.k);
        v0.append(bt.a(this.g));
        return bt.b(v0.toString().getBytes());
    }

    public String toString() {
        int v0 = 1;
        StringBuilder v2 = new StringBuilder();
        v2.append(String.format("version : %s\n", this.d));
        v2.append(String.format("address : %s\n", this.e));
        v2.append(String.format("signature : %s\n", bt.a(this.f)));
        v2.append(String.format("serial : %s\n", Integer.valueOf(this.i)));
        v2.append(String.format("timestamp : %d\n", Integer.valueOf(this.j)));
        v2.append(String.format("length : %d\n", Integer.valueOf(this.k)));
        v2.append(String.format("guid : %s\n", bt.a(this.g)));
        v2.append(String.format("checksum : %s ", bt.a(this.h)));
        String v3 = "codex : %d";
        Object[] v4 = new Object[1];
        if (!this.n) {
            v0 = 0;
        }

        v4[0] = Integer.valueOf(v0);
        v2.append(String.format(v3, v4));
        return v2.toString();
    }
}

